package com.knife.blog.logic;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import com.knife.blog.domain.BlogConfig;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.logic.impl.BlogConfigServiceImpl;
import com.knife.web.Globals;

public class TemplateGenerater {
	private static BlogConfigService blogConfigService = BlogConfigServiceImpl.getInstance();
	
	public static boolean generateTemplate(String[] navigateFile,String path){
		String mainFile = Globals.APP_BASE_DIR + 
			"/WEB-INF/templates/blog/template/common/template.html";
		String contentFile = Globals.APP_BASE_DIR + 
			"/WEB-INF/templates/blog/template/common/content.html";
		return generateTemplate(mainFile,contentFile,navigateFile,path);
	}
	
	public static boolean generateTemplate(BlogInfo blog){
		BlogConfig config = blogConfigService.getBlogConfig(blog.getConfigsn());
		String[] navigate = config.getNavigate().split(",");
		String[] navigateFile = new String[navigate.length];
		for(int i=0;i<navigate.length;i++){
			navigateFile[i] = Globals.APP_BASE_DIR + 
			"/WEB-INF/templates/blog/template/common/navigate-" + navigate[i] + ".html";
		}
		/*String mainFile = Globals.APP_BASE_DIR + 
			"/WEB-INF/knifeweb/blog/template/common/template.html";
		String contentFile = Globals.APP_BASE_DIR + 
			"/WEB-INF/knifeweb/blog/template/common/content.html";*/
		String path = Globals.APP_BASE_DIR + 
			"/WEB-INF/templates/blog/userTemplate/" + blog.getDomain() + "/template.html";
		return generateTemplate(navigateFile,path);
		
	}
	
	public static boolean generateTemplate(String mainFile,
			String contentFile,String[] navigateFile,String path){
		String fileString = mergeFile(mainFile,contentFile,navigateFile);
		Writer writer = null;
		BufferedReader br = null;
		String str = null;
		try {
			File file = new File(path);
			if (!file.getParentFile().exists())
				file.getParentFile().mkdirs();
			writer = new BufferedWriter(
					new OutputStreamWriter(
							new FileOutputStream(path),"utf-8"));
			//writer = new PrintWriter(new BufferedWriter(new FileWriter("source/index2.html")));
			br = new BufferedReader(new StringReader(fileString));
			while((str=br.readLine())!=null){
				writer.write(str+"\n");
				System.out.println(str);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
		
	}
	
	public static String mergeFile(String mainFile,String contentFile,String[] navigatFile){
		String main = file2String(mainFile);
		String content = file2String(contentFile);
		String navigate = file2String(navigatFile);
		main = main.replace("${content}", content);
		main = main.replace("${navigate}", navigate);
		return main;
	}
	
	public static String file2String(String...filename){
		StringBuffer buffer = new StringBuffer();
		BufferedReader reader = null;
		for(String str:filename){
			try {
				reader = new BufferedReader(new InputStreamReader(new FileInputStream(str),"utf-8"));
				loadString(buffer,reader);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}finally{
				if(reader!=null){
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return buffer.toString();
		
	}
	
	protected static void loadString(StringBuffer buffer,BufferedReader br){
		String str;
		try {
			while((str=br.readLine())!=null){
				buffer.append(str+"\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
